package com.zijin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zijin.entity.Frozen513;
import com.zijin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;


@RestController
public class TurnoverController {
    @Autowired
    private Frozen513Service frozen513Service;
    @Autowired
    private Person8Turnover4Service person8Turnover4Service;
    @Autowired
    private Person8Turnover3Service person8Turnover3Service;
    @Autowired
    private Person8Turnover2Service person8Turnover2Service;
    @Autowired
    private Person8Turnover1Service person8Turnover1Service;
    @Autowired
    private Skype264TurnoverService skype264TurnoverService;
    @Autowired
    private Skype412TurnoverService skype412TurnoverService;
    @Autowired
    private Skype1631TurnoverService skype1631TurnoverService;
    @Autowired
    private SkypeBig32TurnoverService skypeBig32TurnoverService;
    @Autowired
    private TheSecondFloorTurnoverService theSecondFloorTurnoverService;
    @Autowired
    private Turnover2159Service turnover2159Service;
    /**
     * 查询冻结卡号
     * */
    @GetMapping("frozen")
    public String getfrozen(String bankno){
        System.out.println(bankno);//18331901040007832
        Frozen513 frozen = frozen513Service.findFrozenByCardNo(bankno);
        if (frozen!=null){
            return frozen.toString();
        }
        return "不存在这张卡";
    }


    /**
    * 查询资金来源
     * 先从八级4人开始查
    * */
    @GetMapping("turnover")
    public JSONObject selTurnover(String countbankno){
        JSONObject res = new JSONObject();
        if(countbankno!=null&&countbankno!=""){
//            从八人四级开始查
            JSONObject fourjsonobj = person8Turnover4Service.findTurnoverAccountNames(countbankno);
            JSONArray links84 = fourjsonobj.getJSONArray("links");
            if (links84.size()>0){
                String count84 = fourjsonobj.getJSONArray("links").getJSONObject(0).getString("target");
                if (count84.length()>0){
                    if (count84!=null&&count84!=""){
//                八人第三层
                        JSONObject threejsonobj = person8Turnover3Service.findTurnoverAccountNames(count84);
                        JSONArray links83 = threejsonobj.getJSONArray("links");
                        if (links83.size()>0) {
                            String count83 = threejsonobj.getJSONArray("links").getJSONObject(0).getString("target");
                            if (count83.length()>0){
                                if (count83!=null&&count83!="") {
//                    八人第二层
                                    JSONObject jsonObject82 = person8Turnover2Service.findTurnoverAccountNames(count83);
                                    JSONArray links82 = jsonObject82.getJSONArray("links");
                                    if (links82.size()>0){
                                        String count1 = jsonObject82.getJSONArray("links").getJSONObject(0).getString("target");
                                        if (count1.length()>0){
                                            if (count1!=null&&count1!=""){
//                                        八人第一层
                                                JSONObject jsonobj1 = person8Turnover1Service.findTurnoverAccountNames(count1);
                                                JSONArray links81 = jsonobj1.getJSONArray("links");
                                                if (links81.size()>0){
                                                    String count81 = jsonobj1.getJSONArray("links").getJSONObject(0).getString("target");
                                                    if (count81.length()>0){
                                                        res = convertJson(fourjsonobj,threejsonobj,jsonObject82,jsonobj1);
                                                    }
                                                }else {
                                                    res = convertJson(fourjsonobj,threejsonobj,jsonObject82);
                                                }
                                            }
                                        }
                                    }else {
                                        res = convertJson(fourjsonobj,threejsonobj);
                                    }
                                }
                            }
                        }else {
                            res = convertJson(fourjsonobj);
                        }
                    }
                }
            }else {
//                查skype412开始
                JSONObject skype412obj = skype412TurnoverService.findTurnoverAccountNames(countbankno);
                JSONArray links412 = skype412obj.getJSONArray("links");
                if (links412.size()>0){
                    String count412 = skype412obj.getJSONArray("links").getJSONObject(0).getString("target");
                    if (count412.length()>0){
                        if (count412!=null&&count412!=""){
//                skype264
                            JSONObject jsonObject264 = skype264TurnoverService.findTurnoverAccountNames(count412);
                            JSONArray links264 = jsonObject264.getJSONArray("links");
                            if (links264.size()>0) {
                                String count264 = jsonObject264.getJSONArray("links").getJSONObject(0).getString("target");
                                if (count264.length()>0){
                                    if (count264!=null&&count264!="") {
//                    skypebig32
                                        JSONObject jsonObject32 = skypeBig32TurnoverService.findTurnoverAccountNames(count264);
                                        JSONArray links32 = jsonObject32.getJSONArray("links");
                                        if (links32.size()>0){
                                            String count32 = jsonObject32.getJSONArray("links").getJSONObject(0).getString("target");
                                            if (count32.length()>0){
                                                res = convertJson(skype412obj,jsonObject264,jsonObject32);
                                            }
                                        }else {
                                            res = convertJson(skype412obj,jsonObject264);
                                        }
                                    }
                                }
                            }else {
                                res = convertJson(skype412obj);
                            }
                        }
                    }
                }else {
//                    从skype1631查
                    JSONObject skype1631obj = skype1631TurnoverService.findTurnoverAccountNames(countbankno);
                    JSONArray links1631 = skype1631obj.getJSONArray("links");
                    if (links1631.size()>0){
                        String count1631 = skype1631obj.getJSONArray("links").getJSONObject(0).getString("target");
                        if (count1631.length()>0){
                            if (count1631!=null&&count1631!=""){
//                                查skype412开始
                                JSONObject skype412objs = skype412TurnoverService.findTurnoverAccountNames(count1631);
                                JSONArray links412s = skype412objs.getJSONArray("links");
                                if (links412s.size()>0){
                                    String count412s1 = skype412objs.getJSONArray("links").getJSONObject(0).getString("target");
                                    if (count412s1.length()>0){
                                        if (count412s1!=null&&count412s1!=""){
//                skype264
                                            JSONObject jsonObject264s = skype264TurnoverService.findTurnoverAccountNames(count412s1);
                                            JSONArray links264s = jsonObject264s.getJSONArray("links");
                                            if (links264s.size()>0) {
                                                String count264s = jsonObject264s.getJSONArray("links").getJSONObject(0).getString("target");
                                                if (count264s.length()>0){
                                                    if (count264s!=null&&count264s!="") {
//                    skypebig32
                                                        JSONObject jsonObjectbig32 = skypeBig32TurnoverService.findTurnoverAccountNames(count264s);
                                                        JSONArray linksbig32 = jsonObjectbig32.getJSONArray("links");
                                                        if (linksbig32.size()>0){
                                                            String countbig32 = jsonObjectbig32.getJSONArray("links").getJSONObject(0).getString("target");
                                                            if (countbig32.length()>0){
                                                                res = convertJson(skype1631obj,skype412objs,jsonObject264s,jsonObjectbig32);
                                                            }
                                                        }else {
                                                            res = convertJson(skype1631obj,skype412objs,jsonObject264s);
                                                        }
                                                    }
                                                }
                                            }else {
                                                res = convertJson(skype1631obj,skype412objs);
                                            }
                                        }
                                    }
                                }else {
                                    res = convertJson(skype1631obj);
                                }
                            }
                        }
                    }else {
//                        从2159查
                        JSONObject turnover2159obj = turnover2159Service.findTurnoverAccountNames(countbankno);
                        JSONArray links2159 = turnover2159obj.getJSONArray("links");
                        if (links2159.size()>0){
                            String count2159 = turnover2159obj.getJSONArray("links").getJSONObject(0).getString("target");
                            if (count2159.length()>0){
                                if (count2159!=null&&count2159!=""){
//                                    查第二层
                                    JSONObject theSecondFloorObj = theSecondFloorTurnoverService.findTurnoverAccountNames(count2159);
                                    JSONArray linkstwo = theSecondFloorObj.getJSONArray("links");
                                    if (linkstwo.size()>0){
                                        String counttwo = theSecondFloorObj.getJSONArray("links").getJSONObject(0).getString("target");
                                        if (counttwo.length()>0){
                                            res = convertJson(turnover2159obj,theSecondFloorObj);
                                        }
                                    }else {
                                        res = convertJson(turnover2159obj);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return res;
    }


    private JSONObject convertJson(JSONObject ... jsons) {
        //        使用jsonobj将数据进行单独封装
        JSONObject job = new JSONObject();
//        JSONArray links = new JSONArray();
//        JSONArray nodes = new JSONArray();
//        HashSet<String> set = new HashSet<>();

        return job;
    }

}
